import { createRouter, createWebHistory } from 'vue-router'
import Layout from '../Layout/MyIndex.vue'
import { isLogin } from '../utils/index'
const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
      {
        path: '/layout',
        name: 'layout',
        component: Layout,
        children: [
          {
            path: '/',
            redirect: '/dashboard'
          },
          {
            path: '/dashboard',
            name: '控制台',
            component: () => import('../views/DashBoard/DashBoard.vue')
          },
          {
            path: '/userinfo',
            name: '用户管理',
            component: () => import('../views/UserInfo/UserInfo.vue')
          },
          {
            path: '/order',
            name: '订单管理',
            component: () => import('../views/OrdersPage/OrdersPage.vue')
          },

          {
            path: '/star',
            name: '明星教师',
            component: () => import('../views/StarTeacher/StarTeacher.vue')
          },
          {
            path: '/active',
            name: '文章管理',
            component: () => import('../views/ActivePage/ActivePage.vue')
          },
          {
            path: '/setting',
            name: '个人中心',
            component: () => import('../views/SettingPage/SettingPage.vue')
          },
          {
            path: '/userinfodetail',
            name: '用户信息',
            component: () => import('../views/UserInfoDetailPage/UserInfoDetailPage.vue')
          }
        ]
      },
      {
        path: '/login',
        name: 'login',
        component: () => import('../views/LoginPage/LoginPage.vue')
      },
      {
        path: '/:pathMatch(.*)*',
        name: 'notFound',
        meta: {
          roles: '*'
        },
        component: () => import('../views/NotFound/NotFound.vue')
      }
    ]
  })
  
  router.beforeEach((to, from, next) => {
    if (to.path !== '/login') {
      if (isLogin()) {
        next()
      } else {
        router.replace('/login')
      }
    } else {
      next()
    }
  })
  export default router